package ZaznaczObszar;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class testokna2 extends JPanel {
	JDialog jd;
	JButton okButton, cancelBuuton;
	boolean ok;
	
	BufferedImage buf;
	Robot rob;
	
	public testokna2(){
		setLayout(new BorderLayout());
		
		JPanel jp = new JPanel();
		jp.setLayout(new GridLayout(3, 2));
		jp.add(new JButton("ok"));
		jp.add(new JButton("cancel"));
		jp.add(new JButton("cancel"));

		jp.add(new JButton("cancel"));
		jp.add(new JButton("cancel"));
		jp.add(new JButton("cancel"));
		jp.add(new JButton("cancel"));
		
		//add(jp, BorderLayout.CENTER);
		
		
		okButton = new JButton ("ok");
		okButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				ok = true;
				jd.setVisible(false);				
			}
		});
		
		cancelBuuton = new JButton("cancel");
		cancelBuuton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jd.setVisible(false);
			}
		});
		
		try {
			rob = new Robot();
		} catch (AWTException e1) {
			e1.printStackTrace();
		}
		buf = rob.createScreenCapture(new Rectangle(100,100,100,100));
		
		JPanel buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridLayout(1,2));
			buttonPanel.add(okButton);
			buttonPanel.add(cancelBuuton);
		add(buttonPanel, BorderLayout.SOUTH);

		JPanel obr = new JPanel();
			obr.setLayout(null);
			obr.setBackground(Color.gray);
			obr.add(new obraz());		
		add(obr, BorderLayout.CENTER);
		
		//add(new obraz());	
			
		repaint();
		revalidate();
	}
	
	private Component createJPanel(){
		return cancelBuuton;
		
	}
	
	class obraz extends JPanel {
		BufferedImage buf;
		Rectangle rec;
		Canvas can;
		
		public obraz(){
			super();
			can = new Canvas();
			rec = new Rectangle(10, 10, 150, 100);
			//setBounds(rec);
			setSize(100, 100);
			setBackground (Color.CYAN);
			setLayout(new BorderLayout());
			//setForeground(Color.GRAY);
		}
		
		@Override
		protected void paintComponent(Graphics g) {
			//g.drawImage(buf, 0, 0, null);
			g.setColor(Color.BLUE);
			g.drawOval(5, 5, 10, 10);
			super.paintComponent(g);
		}
	}


	public boolean okno(Component parent, String title){
		
		Frame owner = null;
		if (parent instanceof Frame) owner = (Frame) parent;
		else owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
		
		if (jd == null || jd.getOwner()!=owner){
			jd = new JDialog(owner, true);
			jd.add(this);
			jd.getRootPane().setDefaultButton(okButton);
			jd.setSize(450, 200);
			//jd.pack();
		}
		
		jd.setVisible(true);
		jd.setTitle(title);
		
		return true;

	}

	public static void main(String[] args) {
		System.out.println ("wyszedlem");
		new testokna2().okno(null, "test");
		//JOptionPane.showConfirmDialog(null, "OBRAZY S� NIE ZGONE WIELKO�CIOWO");
		System.out.println ("wyszedlem");
	
	}
	
	



}
